package service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import security.MySecurityContextHandle;
import service.interf.IIfwSrouterMap;
import service.interf.IOperationLog;

import common.impl.exception.ServiceException;

import dao.interf.IIfwSrouterMapDAO;
import dao.pojo.IfwSrouterMap;

public class IfwSrouterMapImpl extends BaseDataServiceImpl implements
		IIfwSrouterMap {

	private IIfwSrouterMapDAO ifwSrouterMapDao;
	private IOperationLog iOperationLog;

	public void setIfwSrouterMapDao(IIfwSrouterMapDAO ifwSrouterMapDao) {
		this.ifwSrouterMapDao = ifwSrouterMapDao;
	}

	public void setiOperationLog(IOperationLog iOperationLog) {
		this.iOperationLog = iOperationLog;
	}

	public String add(String rlyModel, String deviceModel, String sn,
			String supplier, String note) throws Exception {
		// 验证
		String userName = MySecurityContextHandle.getUserName();
		if (!"admin".equals(userName))
			throw new ServiceException("请先登录");
		if (StringUtils.isEmpty(rlyModel))
			throw new ServiceException("蜂网软路由型号不能为空");
		if (StringUtils.isEmpty(deviceModel))
			throw new ServiceException("设备CPU型号不能为空");
		if (StringUtils.isEmpty(sn))
			throw new ServiceException("设备SN不能为空");
		if (StringUtils.isEmpty(supplier))
			throw new ServiceException("设备提供商不能为空");
		long total = ifwSrouterMapDao.getPropertiesCount(new String[] { "fwsn",
				"sn" }, new String[] { sn, sn }, new String[] { "or" }, null,
				null);
		if (total > 0)
			throw new ServiceException("设备" + sn + "已登记！不用重复登记！");
		rlyModel = rlyModel.toUpperCase();
		String fwsn = rlyModel + "-";
		deviceModel = deviceModel.toUpperCase();
		if ("D525".equals(deviceModel)) {
			fwsn += "5";
		} else if ("1037U".equals(deviceModel)) {
			fwsn += "1";
		} else {
			throw new ServiceException("设备CPU型号参数错误");
		}
		List<IfwSrouterMap> mapList = ifwSrouterMapDao.findByPropertiesRule(
				new String[] { "fwsn" }, new Object[] { fwsn + "%" },
				new String[] { "like" }, " order by model.fwsn desc", 0, 1);
		if (null != mapList && !mapList.isEmpty()) {
			String fwsn_temp = mapList.get(0).getFwsn();
			if (!StringUtils.isEmpty(fwsn_temp)) {
				fwsn_temp = fwsn_temp.substring(5);
				int fwsn_int = Integer.parseInt(fwsn_temp);
				fwsn_int += 1;
				String fwsn_end = String.valueOf(fwsn_int);
				int num0 = 9 - fwsn_end.length();
				while (num0 > 0) {
					fwsn += "0";
					num0--;
				}
				fwsn += fwsn_end;
			} else {
				fwsn += "000000001";
			}
		} else {
			fwsn += "000000001";
		}
		// 备注
		String time = myDateHandle.MyStringToISODateTimeString(myDateHandle
				.getCurrentDTString());
		if (StringUtils.isEmpty(note)) {
			note = "";
		} else {
			note += "-";
		}
		note += supplier + "-" + time;
		IfwSrouterMap map = new IfwSrouterMap();
		map.setFwsn(fwsn);
		map.setSn(sn);
		map.setNote(note);
		ifwSrouterMapDao.save(map);
		iOperationLog.add(1, fwsn, sn, null, null, deviceModel, supplier);
		return "1";
	}
}
